package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    //明细查询
    @GetMapping("/detail")
    public Object detail(String id) {
        Strategy strategy = strategyService.get(id);
        //攻略的阅读数
        strategyStatisVORedisService.viewnumIncrease(id,1);
        return JsonResult.success(strategy);
    }
    //分页查询
    @GetMapping("/query")
    public Object query(StrategyQuery qo) {
        Page<Strategy> query = strategyService.query(qo);
        return JsonResult.success(query);
    }
    //所有主题
    @GetMapping("/themes")
    public Object themes() {

        return JsonResult.success(strategyThemeService.list());
    }
    //添加攻略评论
    @PostMapping("/addComment")
    @RequireLogin
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo) {
        //使用属性拷贝
        BeanUtils.copyProperties(userInfo,strategyComment);
        strategyComment.setUserId(userInfo.getId());
        strategyCommentService.save(strategyComment);
        strategyStatisVORedisService.replynumIncrease(strategyComment.getStrategyId(),1);
        return JsonResult.success();
    }
    //攻略评论分页
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo) {
        return JsonResult.success(strategyCommentService.query(qo));
    }
    //攻略评论点赞
    @PostMapping("/commentThumb")
    @RequireLogin
    public Object commentThumb(String  cid,@UserParam UserInfo userInfo ) {
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        //根据攻略id查统计数据vo对象
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }
    @PostMapping("/favor")
    @RequireLogin
    public Object favor(String sid,@UserParam UserInfo userInfo) {
        //攻略收藏
        boolean ret = strategyStatisVORedisService.favor(sid, userInfo.getId());
        return JsonResult.success(ret);
    }
    @PostMapping("/strategyThumbup")
    @RequireLogin
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo) {
        //顶
        boolean ret = strategyStatisVORedisService.strategyThumbup(sid, userInfo.getId());
        return JsonResult.success(ret);
    }


}
